package com.xsf.jieyou.provider.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xsf.jieyou.provider.api.ProviderTbUserLogService;
import com.xsf.jieyou.provider.domain.TbUserLog;
import com.xsf.jieyou.provider.fallback.TbUserLogServiceFallback;
import com.xsf.jieyou.provider.mapper.TbUserLogMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户日志，Dubbo服务接口实现。
 * <p>
 * Description:Dubbo服务接口实现。
 * </p>
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/11/15 0015
 * @see com.xsf.jieyou.provider.service
 **/
@Service(version = "1.0.0")
public class ProviderTbUserLogServiceImpl implements ProviderTbUserLogService {

    @Resource
    private TbUserLogMapper tbUserLogMapper;

    /**
     * 新增用户日志
     *
     * @param tbUserLog {@link TbUserLog} 用户日志对象
     * @return {@link Integer} 1：success  0：fall   -1：熔断
     */
    @Override
    @SentinelResource(value = "insertTbUserLog", fallbackClass = TbUserLogServiceFallback.class, fallback =
            "insertTbUserLogFallback")
    public int insert(TbUserLog tbUserLog) {
        // ############   测试熔断（success）   #################################
        /*if (1 + 1 == 2) {
            throw new RuntimeException();
        }*/
        // ############   测试熔断   #################################

        // 初始化用户日志对象
        this.initTbUserLog(tbUserLog);
        return this.tbUserLogMapper.insert(tbUserLog);
    }

    /**
     * 初始化用户日志对象
     *
     * @param tbUserLog {@link TbUserLog} 用户日志对象
     */
    private void initTbUserLog(TbUserLog tbUserLog) {
        tbUserLog.setCreateTime(new Date());
    }
}
